package com.client.task;

import com.client.bean.Instance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.TimerTask;

/**
 * Description:
 *
 * @author weiruibai.vendor
 * Date: 2023/1/28 11:20
 */
@Slf4j
public class MyTask extends TimerTask {

    private Instance instance;
    private static final String ADD = "http://localhost:7001/server/add";

    public MyTask(Instance instance) {
        this.instance = instance;
    }

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void run() {
        log.info("---------------------------start task---------------------------");
        /**
         * 注册实例到server
         */
        log.info("start add instance to eureka server");
        registerInstance();
        log.info("end add instance to eureka server");

        log.info("---------------------------end task---------------------------");
    }

    private void registerInstance() {
        try {
            Instance body = restTemplate.postForEntity(ADD, instance, Instance.class).getBody();
        } catch (Exception e) {
            log.error("{}", e);
        }
    }
}
